﻿# -*- coding:utf-8 -*-
from django.db import models
from django.contrib.auth.models import User


class Severity(models.Model):
    title = models.CharField(max_length=32, verbose_name='Важность задачи')
    description = models.TextField(blank=True, verbose_name='Описание')
    order = models.IntegerField()
    default = models.BooleanField(verbose_name="По умолчанию")
     
    class Meta:
        ordering = ['order']
    
    def __unicode__(self):
        return self.title


class Statusresolution(models.Model):
    title = models.CharField(max_length=32, verbose_name='Основание')
    description = models.TextField(blank=True, verbose_name='Описание')
   
     
    class Meta:
        ordering = ['title']
    
    def __unicode__(self):
        return self.title

class Status(models.Model):
    title = models.CharField(max_length=32, verbose_name='Статус задачи')
    description = models.TextField(blank=True, verbose_name='Описание')
    next = models.ManyToManyField('self', symmetrical=False, blank=True, null=True, related_name = 'self_status_set') 
    resolution = models.ForeignKey(Statusresolution, blank=True, null=True, verbose_name='Основание изменения') 
    default = models.BooleanField(verbose_name="По умолчанию") 
     
    class Meta:
        ordering = ['title']
    
    def __unicode__(self):
        return self.title


class Kind(models.Model):
    title = models.CharField(max_length=32, verbose_name='Тип задачи')
    description = models.TextField(blank=True, verbose_name='Описание')
     
    class Meta:
        ordering = ['title']
    
    def __unicode__(self):
        return self.title	


class Task(models.Model):
    kind = models.ForeignKey(Kind, verbose_name='Тип задачи')
    summary = models.CharField(max_length=100, verbose_name='Задача')
    description = models.TextField(blank=True, verbose_name='Описание')
    status = models.ForeignKey(Status, verbose_name='Статус')
    severity = models.ForeignKey(Severity, verbose_name='Важность')
    reportdate = models.DateTimeField(auto_now_add=True, verbose_name='Дата')
    reporter = models.ForeignKey(User, verbose_name='Создатель',related_name = 'task_reporter_set')
    owner = models.ForeignKey(User, verbose_name='Владелец',related_name = 'task_owner_set', default=None)
    resolution = models.TextField(blank=True, verbose_name='Причина закрытия') 
    
    date_start = models.DateTimeField(blank=True, null=True, verbose_name='Дата начала')
    date_end = models.DateTimeField(blank=True, null=True, verbose_name='Срок выполнения')
    
    person = models.CharField(max_length=64, blank=True, verbose_name='Контактное лицо') 
    department = models.CharField(max_length=64, blank=True, verbose_name='Отдел')
    room = models.CharField(max_length=64, blank=True, verbose_name='Комната')
    phone = models.CharField(max_length=64, blank=True, verbose_name='Телефон')
    
 
    class Meta:
        ordering = ['kind']
    
    def __unicode__(self):
        return self.summary		



class History(models.Model):
    task = models.ForeignKey(Task, verbose_name='Задача')
    action = models.TextField(editable=False, blank=True)
    comment = models.TextField(verbose_name='Коментарий', blank=True)
    date = models.DateTimeField(auto_now_add=True, verbose_name='Дата')
    user = models.ForeignKey(User, verbose_name='Пользователь', related_name = 'history_user_set')
    
    comment.allow_tags = True
    action.allow_tags = True

    class Meta:
        ordering = ['date']
    

    def __unicode__(self):
        return self.comment




